package com.bxs.demo;

import org.pcap4j.core.*;
import org.pcap4j.packet.IcmpV4CommonPacket;
import org.pcap4j.packet.IpV4Packet;
import org.pcap4j.packet.TcpPacket;
import org.pcap4j.packet.namednumber.IcmpV4Type;
import org.pcap4j.packet.namednumber.IpNumber;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PcapTest {
    public static void main(String[] args) throws UnknownHostException, PcapNativeException, NotOpenException {
        PcapNetworkInterface nif = Pcaps.getDevByAddress(InetAddress.getByName("192.168.1.101"));
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        PcapHandle handle = nif.openLive(65536, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, 10);
        PacketListener listener = new PacketListener() {
            @Override
            public void gotPacket(PcapPacket packet) {
                IpV4Packet ipV4Packet = packet.get(IpV4Packet.class);
                if(ipV4Packet == null)return;
                IpNumber protocol = ipV4Packet.getHeader().getProtocol();
                String name = protocol.name();
                String hostName = ipV4Packet.getHeader().getDstAddr().getHostName();
                String hostName1 = ipV4Packet.getHeader().getSrcAddr().getHostName();
                System.out.print(name + "; " + "; " + hostName + "; " + hostName1);
                if(name.equals(IpNumber.ICMPV4.name())){
                    IcmpV4CommonPacket icmpV4CommonPacket = ipV4Packet.get(IcmpV4CommonPacket.class);
                    IcmpV4Type type = icmpV4CommonPacket.getHeader().getType();
                    System.out.print("icmp type:" + type.valueAsString());
                }else if(name.equals("TCP")) {
                    TcpPacket packets = ipV4Packet.get(TcpPacket.class);
                    if (packets == null) return;
                    TcpPacket.TcpHeader header = packets.getHeader();
                    int srcPort = header.getSrcPort().valueAsInt();
                    int dstPort = header.getDstPort().valueAsInt();
                    System.out.print(";" + name + "; " + srcPort + "; " + dstPort);
                }
                System.out.println();
            }
        };

        // 直接使用loop无限循环处理包
        try {
            handle.setFilter("icmp", BpfProgram.BpfCompileMode.OPTIMIZE);
            handle.loop(2000, listener, executorService); // COUNT设置为抓包个数，当为-1时无限抓包
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (NotOpenException e) {
            e.printStackTrace();
        }
        handle.breakLoop();
        executorService.shutdown();
        PcapStat stats = handle.getStats();
        System.out.println("received: " + stats.getNumPacketsReceived());
        System.out.println("captured: " + stats.getNumPacketsCaptured());
        System.out.println("dropped: " + stats.getNumPacketsDropped());

        handle.close();


    }
}
